import Base from 'models/base';

import moment from 'moment';

export default class GoodsActivityForm extends Base {
  constructor(goods) {
    super();

    if (goods === undefined) {
      goods = {};
      this.status = 1;
      this.startTime = null;
      this.endTime = null;
      this.branchId = '';
      this.partnerId = '';
    } else {
      this.id = goods.recommendGoodsId;
      this.status = Number(goods.status);
      this.startTime = moment(goods.activityStartTime).valueOf();
      this.endTime = moment(goods.activityEndTime).valueOf();
    }
    let fields = ['activityTypeId', 'goodsDefId', 'goodsName', 'branchName', 'partnerName'];
    this.initObject(goods, fields);
  }

  checkValid() {
    let fields = ['goodsDefId', 'activityTypeId', 'startTime', 'endTime'];
    let code = this.checkRequired(fields);
    if (code !== '') {
      return code;
    }

    let startTime = this.startTime;
    let endTime = this.endTime;
    if (moment(startTime).isAfter(endTime, 'second')) {
      return 'startTime>endTime';
    }
    return '';
  }

  getObject() {
    let postData = {};
    let fields = ['activityTypeId', 'goodsDefId', 'status'];
    this.setPostData(postData, fields);

    let startTime = this.startTime;
    postData.activityStartTime = moment(startTime).format('YYYY-MM-DD HH:mm:ss');

    let endTime = this.endTime;
    postData.activityEndTime = moment(endTime).format('YYYY-MM-DD HH:mm:ss');

    let id = this.id;
    if (id !== undefined) {
      postData.recommendGoodsId = id;
    }

    return postData;
  }
}
